package com.onlyxiahui.common.lib.util.http;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.protocol.HTTP;

/**
 * 基于 httpclient4版本的http工具类<br>
 * Date 2019-01-06 18:39:35<br>
 * 
 * @author XiaHui<br>
 * @since 1.0.0
 */
public class HttpClient4PoolingUtil {

	public static final HttpClientPoolingAdaptor HTTP_CLIENT = new HttpClientPoolingAdaptor();

//	static {
//		// https config
//		TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
//		SSLContext sslContext = null;
//		try {
//			sslContext = org.apache.http.ssl.SSLContexts.custom()
//					.loadTrustMaterial(null, acceptingTrustStrategy)
//					.build();
//		} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
//			e.printStackTrace();
//		}
//
//		SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, null, null, NoopHostnameVerifier.INSTANCE);
//
//		Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
//				.register("http", PlainConnectionSocketFactory.getSocketFactory())
//				.register("https", csf)
//				.build();
//
//		PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
//		// 最大连接数3000
//		connectionManager.setMaxTotal(3000);
//		// 路由链接数400
//		connectionManager.setDefaultMaxPerRoute(400);
//		RequestConfig requestConfig = RequestConfig.custom()
//				.setSocketTimeout(60000)
//				.setConnectTimeout(60000)
//				.setConnectionRequestTimeout(10000)
//				.build();
//
//		HTTP_CLIENT = HttpClients.custom().setDefaultRequestConfig(requestConfig)
//				.setConnectionManager(connectionManager)
//				.evictExpiredConnections()
//				.evictIdleConnections(30, TimeUnit.SECONDS)
//				.build();
////		RequestConfig config = RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(15000).build();
////		HTTP_CLIENT = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
////        httpClient =HttpClients.custom().setUserAgent("fbadp").setDefaultRequestConfig(config).build();
//
//	}

	/******************* Get *********************/

	public static String doGet(String url) {
		return doGet(url, Collections.emptyMap(), Collections.emptyMap(), null, null);
	}

	public static String doGet(String url, Map<String, Object> params) {
		return doGet(url, Collections.emptyMap(), params, null, null);
	}

	public static String doGet(String url, Map<String, String> headers, Map<String, Object> params) {
		return doGet(url, headers, params, null, null);
	}

	public static String doGet(String url, Map<String, String> headers, Map<String, Object> params, String requestCharset) {
		return doGet(url, headers, params, requestCharset, null);
	}

	public static String doGet(
			String url,
			Map<String, String> headers,
			Map<String, Object> params,
			String requestCharset,
			String responseCharset) {
		return HTTP_CLIENT.doGet(url, headers, params, requestCharset, responseCharset);
	}

	/******************* Post body *********************/

	public static String doPost(String url, String body) {
		return doPost(url, Collections.emptyMap(), body, null, null);
	}

	public static String doPost(String url, Map<String, String> headers, String body) {
		return doPost(url, headers, body, null, null);
	}

	public static String doPost(String url, Map<String, String> headers, String body, String requestCharset) {
		return doPost(url, headers, body, requestCharset, null);
	}

	public static String doPost(
			String url,
			Map<String, String> headers,
			String body,
			String requestCharset,
			String responseCharset) {
		return HTTP_CLIENT.doPost(url, headers, body, requestCharset);
	}

	/******************* Post params *********************/
	public static String doPost(String url) {
		return doPost(url, Collections.emptyMap(), Collections.emptyMap(), null, null);
	}

	public static String doPost(String url, Map<String, Object> params) {
		return doPost(url, Collections.emptyMap(), params, null, null);
	}

	public static String doPost(String url, Map<String, String> headers, Map<String, Object> params) {
		return doPost(url, headers, params, null, null);
	}

	public static String doPost(String url, Map<String, String> headers, Map<String, Object> params, String requestCharset) {
		return doPost(url, headers, params, requestCharset, null);
	}

	public static String doPost(String url,
			Map<String, String> headers,
			Map<String, Object> params,
			String requestCharset,
			String responseCharset) {
		return HTTP_CLIENT.doPost(url, headers, params, requestCharset, responseCharset);
	}

	/*********/

	public static String jsonPost(String url, String body) {
		return jsonPost(url, null, body);
	}

	public static String jsonPost(String url, Map<String, String> headers, String body) {
		Map<String, String> map = null == headers ? new HashMap<>() : headers;
		map.put(HTTP.CONTENT_TYPE, "application/json; charset=UTF-8");
		map.put("Accept", "application/json; charset=UTF-8");
		return doPost(url, map, body, null, null);
	}
}
